//
//  HuaBaoViewController.m
//  Style
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HuaBaoViewController.h"
#import "HuaBaoView.h"
#import <Masonry.h>
#import "LSQInterestTableViewCell.h"
#import "LSQInterestTooTableViewCell.h"
#import "HuaBaoModel.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "LSQPushViewController.h"
#import <MJRefresh.h>
#import "LSQPushTooViewController.h"
#import "LSQNoPicCell.h"
@interface HuaBaoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) UISegmentedControl *segmen;
@property (nonatomic ,strong) NSMutableArray *arrayInterest;
@property (nonatomic ,strong) UITableView *tableViewInterest;
@property (nonatomic ,strong) UITableView *tableViewTooInterest;
@property (nonatomic ,strong) NSMutableDictionary *dicTop;
@property (nonatomic ,strong) HuaBaoView *topView;
@property (nonatomic ,strong) NSMutableArray *arrayInterestToo;
@property (nonatomic ,retain) UIActivityIndicatorView *activityIndicator;


@end

@implementation HuaBaoViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"生活有意思";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self tableViewSubView];
    [self dataHandel];
    [self segmenSubView];
    
}
-(void)segmenSubView
{
    self.segmen = [[UISegmentedControl alloc]initWithItems:@[@"有意思",@"也有意思"]];
    [self.view addSubview:self.segmen];
    self.segmen.selectedSegmentIndex = 0;/*根据下标 选取内容*/
    self.segmen.tintColor = [UIColor blackColor];//改变颜色
    self.segmen.frame = CGRectMake(1,  0 , self.view.frame.size.width - 2 , self.view.frame.size.height/(667/40));
/*给segmen 添加点击事件*/
    [self.segmen addTarget:self action:@selector(segmenButton:) forControlEvents:UIControlEventValueChanged];
    
}
-(void)tableViewSubView
{
 /*大的ScrollView*/
    
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/66.7)];
        [self.view addSubview:self.scrollView];

        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
        self.scrollView.pagingEnabled = YES;
        
        self.scrollView.scrollEnabled = NO;
        
    /*有意思的 tableView*/
    self.tableViewInterest = [[UITableView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/(self.view.frame.size.height/40), self.view.frame.size.width, self.view.frame.size.height  - self.view.frame.size.height/(self.view.frame.size.height/65)) style:UITableViewStyleGrouped];
    self.tableViewInterest.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:self.tableViewInterest];
    self.tableViewInterest.delegate =self;
    self.tableViewInterest.dataSource = self;
    //self.tableViewInterest.separatorStyle = 0;
    //self.tableViewInterest.backgroundColor = [UIColor blueColor];
    
    
    
    
    /*下拉刷新*/
    self.tableViewInterest.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    /*上啦加载*/
    self.tableViewInterest.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    /*也有意思的 tableView*/
    self.tableViewTooInterest = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.size.height/(ScreenHeight/40), self.view.frame.size.width, self.view.frame.size.height  - self.view.frame.size.height/ (ScreenHeight / 110) ) style:UITableViewStylePlain];
    
    [self.scrollView addSubview:self.tableViewTooInterest];
    self.tableViewTooInterest.delegate =self;
    self.tableViewTooInterest.dataSource = self;
    //self.tableViewTooInterest.separatorStyle = 0;
    
    /*下拉刷新*/
    self.tableViewTooInterest .header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDataToo)];
    /*上啦加载*/
    self.tableViewTooInterest.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDataToo)];
   
}
/*有意思  刷新的数据*/
- (void)loadNewData{
    
    [self.tableViewInterest.header beginRefreshing];
    /*********有意思的数据**************************/
    self.arrayInterest = [NSMutableArray array];
    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
    [manger GET:@"http://u14.mmbang.com/open/article/list/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2&mobile_os=ios" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            HuaBaoModel *model = [[HuaBaoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayInterest addObject:model];
            
        }
        
        [self.tableViewInterest reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        }];
    
    [self.tableViewInterest.header endRefreshing];
    
}
/*有意思 上拉加载的数据*/
-(void)loadMoreData

{
    [self.tableViewInterest.footer beginRefreshing];
    HuaBaoModel *model1 = [self.arrayInterest lastObject];
    
    /*********有意思的数据**************************/
   // self.arrayInterest = [NSMutableArray array];
    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
    NSString *str = [NSString stringWithFormat:@"http://u14.mmbang.com/open/article/list/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2&mobile_os=ios&order=%@",model1.order];
    [manger GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            HuaBaoModel *model = [[HuaBaoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayInterest addObject:model];
            }
        
        [self.tableViewInterest reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
    [self.tableViewInterest.footer endRefreshing];
}


/*也有意思  刷新的数据*/
- (void)loadNewDataToo
{
    
    [self.tableViewTooInterest.header beginRefreshing];
    /*********也有意思的数据**************************/
//    self.arrayInterestToo = [NSMutableArray array];
//    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
//    [manger GET:@"http://u14.mmbang.com/open/stream/posts/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        NSMutableArray *array = [NSMutableArray array];
//        
//        array = [responseObject objectForKey:@"data"];
//        
//        for (NSMutableDictionary *dic in array) {
//            
//            LSQpushModel *model = [[LSQpushModel alloc]init];
//            [model setValuesForKeysWithDictionary:dic];
//            
//            [self.arrayInterestToo addObject:model];
//            
//        }
//        
//        [self.tableViewTooInterest reloadData];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//        NSLog(@"%@",error);
//    }];
    
    [self.tableViewTooInterest.header endRefreshing];
    
}
/*也有意思 上拉加载的数据*/
-(void)loadMoreDataToo

{
    [self.tableViewTooInterest.footer beginRefreshing];
    LSQpushModel *model1 = [self.arrayInterestToo lastObject];
    
    /********* 也有意思的数据**************************/
    // self.arrayInterest = [NSMutableArray array];
    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
    NSString *str = [NSString stringWithFormat:@"http://u14.mmbang.com/open/stream/posts/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2&lt=%@",model1.added_on];
    [manger GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            LSQpushModel *model = [[LSQpushModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayInterestToo addObject:model];
        }
        
        [self.tableViewTooInterest reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
    [self.tableViewTooInterest.footer endRefreshing];
}



-(void)segmenButton:(UISegmentedControl *) segmen
{
    
    if (segmen.selectedSegmentIndex == 1) {
        
     [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width,0) animated:YES];
    
        
    }else
    {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    }
}

-(void)dataHandel
{
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] init];
    self.activityIndicator.frame = CGRectMake(0, 0, 50, 50);
    self.activityIndicator.center = self.view.center;
   [self.view addSubview:_activityIndicator];
     self.activityIndicator.color = [UIColor blackColor];
    [_activityIndicator startAnimating];
    
 /************有意思的头部数据********************************/
    self.dicTop =[ NSMutableDictionary dictionary];
    AFHTTPRequestOperationManager *mangerTop = [[AFHTTPRequestOperationManager alloc]init];
    [mangerTop GET:@"http://u14.mmbang.com/open/article/list/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2&featured=1&mobile_os=ios" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            HuaBaoModel *model = [[HuaBaoModel alloc]init];
            self.dicTop = dic;
            [model setValuesForKeysWithDictionary:dic];
            }
        
        [self.tableViewInterest reloadData];
        [_activityIndicator stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [_activityIndicator stopAnimating];
        
    }];

    /*********有意思的数据**************************/
    self.arrayInterest = [NSMutableArray array];
    AFHTTPRequestOperationManager *manger = [[AFHTTPRequestOperationManager alloc]init];
    [manger GET:@"http://u14.mmbang.com/open/article/list/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2&mobile_os=ios" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            HuaBaoModel *model = [[HuaBaoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayInterest addObject:model];
            
        }
        
        [self.tableViewInterest reloadData];
        [_activityIndicator stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
        NSLog(@"%@",error);
         [_activityIndicator stopAnimating];
    }];
   
    
    /*************也有意思的数据***************/
    self.arrayInterestToo = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://u14.mmbang.com/open/stream/posts/?device=451866cd23b2b811dfedc8657d4d8a0c638042d2" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [responseObject objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in array) {
            
            LSQpushModel *model = [[LSQpushModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayInterestToo addObject:model];
            
        }
        
        [self.tableViewTooInterest reloadData];
        [_activityIndicator stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [_activityIndicator stopAnimating];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    int x = tableView.frame.origin.x / self.view.frame.size.width;
    if (x == 0) {
        
    return self.arrayInterest.count;
    } if (x==1){
    
        return self.arrayInterestToo.count ;
    }else
    {
        return 0;
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int x = tableView.frame.origin.x / self.view.frame.size.width;
    if (x == 0) {
        
        return 1;
    }else
    {
    
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int x = tableView.frame.origin.x / self.view.frame.size.width;
    if (x == 0) {
    
        LSQInterestTableViewCell *interestCell  = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
        if (self.arrayInterest.count != 0) {
            
      
        
    if (interestCell == nil) {
        
        interestCell = [[LSQInterestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        
    
    }
    
    HuaBaoModel *model = [[HuaBaoModel alloc]init];
    
    model = [self.arrayInterest objectAtIndex:indexPath.row];

    
    interestCell.model = model;
    
    interestCell.selectionStyle = 0;
     }
    return interestCell;
        
    }  else
    {
       
        LSQpushModel *model = [[LSQpushModel alloc]init];
        
        model = [self.arrayInterestToo objectAtIndex:indexPath.row];
        //NSLog(@"%@====%@", model.author, model.thumb);

        if ([model.thumb isEqualToString:@""]) {
            
            LSQNoPicCell *cellNo = [tableView dequeueReusableCellWithIdentifier:@"reuseNo"];
            
            if (self.arrayInterestToo.count != 0) {

            if (cellNo == nil) {
                cellNo = [[LSQNoPicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseNo"];
            }
            NSLog(@"1111111111111111111111");
            cellNo.model = model;
            
            cellNo.selectionStyle = 0;
            }
            return cellNo;
            
        }
        else{
        
        
        LSQInterestTooTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
        
            if (self.arrayInterestToo.count != 0) {
                
           
        if (cell ==  nil) {
            
            cell = [[LSQInterestTooTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse1"];
        }
     
        cell.model = model;
        cell.selectionStyle = 0;
        cell.imageViewThumb.contentMode = UIViewContentModeScaleAspectFit;
        cell.imageViewThumb.autoresizesSubviews = YES;
        cell.imageViewThumb.autoresizingMask =
        UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
                 }
        return cell;

        }
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int x = tableView.frame.origin.x / self.view.frame.size.width;
    
    if (x == 0) {
        
        return ScreenHeight / ([UIScreen mainScreen].bounds.size.height / 120);
    }
    else
        {
            
            LSQpushModel *model = [[LSQpushModel alloc]init];
            
            model = [self.arrayInterestToo objectAtIndex:indexPath.row];
            
            if ([model.thumb isEqualToString:@""]) {
                
                
                NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
                CGRect rect = [[self.arrayInterestToo[indexPath.row]content ]boundingRectWithSize:CGSizeMake(ScreenWidth - ScreenHeight / (ScreenHeight/ 30) , 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
                
                return [UIScreen mainScreen].bounds.size.height / ([UIScreen mainScreen].bounds.size.height / rect.size.height)+  ScreenHeight / ([UIScreen mainScreen].bounds.size.height / 220) -  ScreenHeight / ([UIScreen mainScreen].bounds.size.height /160);

                }
            
            
       NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
            CGRect rect = [[self.arrayInterestToo[indexPath.row]content ]boundingRectWithSize:CGSizeMake(ScreenWidth - ScreenHeight / (ScreenHeight/ 30) , 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
            
            return [UIScreen mainScreen].bounds.size.height / ([UIScreen mainScreen].bounds.size.height / rect.size.height) +  ScreenHeight / ([UIScreen mainScreen].bounds.size.height / 220) ;
    }

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    int x = tableView.frame.origin.x / self.view.frame.size.width;
    
    if (x==0)
    {
        if (section == 0) {
        
        return ScreenHeight/([UIScreen mainScreen].bounds.size.height / 220);
        
    }else
    {
        return 0;
    }
    

    }else
    {
        return 0;
    
    }
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   int x = tableView.frame.origin.x / self.view.frame.size.width;
    
    if (x == 0) {
    
        
    HuaBaoModel *model = [[HuaBaoModel alloc]init];
        [model setValuesForKeysWithDictionary:self.dicTop];
        
    
    self.topView = [[HuaBaoView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight / (ScreenHeight / 150))];
    
    [self.topView.imageViewTop sd_setImageWithURL:[NSURL URLWithString:model.icon_url] placeholderImage:nil];
    
    self.topView.labelTitle.text = model.title;
    self.topView.lableSummary.text = model.summary;
    
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    /*设置相关属性*/
    tap4.numberOfTapsRequired = 1;//按的次数
    tap4.numberOfTouchesRequired = 1;//手指个数
    /*将手势加在 视图上,注意视图的用户交互属性 是否开启*/
    [self.topView.imageViewTop addGestureRecognizer:tap4];
    self.topView.imageViewTop.userInteractionEnabled = YES;/*用户交互 打开*/
    
    return self.topView;
    }else
    {
        return nil;
    
    }

}

-(void)tapAction
{
    LSQPushViewController *push = [[LSQPushViewController alloc]init];
    
    HuaBaoModel *model = [[HuaBaoModel alloc]init];
    
    [model setValuesForKeysWithDictionary:self.dicTop];
    
     push.url = model.web_url;
    
    [self.navigationController pushViewController:push animated:YES];


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     int x = tableView.frame.origin.x / self.view.frame.size.width;
    
    
    if (x == 0) {
        LSQPushViewController *push = [[LSQPushViewController alloc]init];
    
    HuaBaoModel *model = [[HuaBaoModel alloc]init];
    
    model =  [self.arrayInterest objectAtIndex:indexPath.row];
    
    //push.array = model.paragraphs;
    push.name = model.title;
    push.url = model.web_url;
    [self.navigationController pushViewController:push animated:YES];
        
 }else
 {
 
     LSQpushModel *model = [[LSQpushModel alloc]init];
     
     model = [self.arrayInterestToo objectAtIndex:indexPath.row];
     
     if ([model.thumb isEqualToString:@""]) {
 
         NSLog(@"123456789");
         
         
     }else{
     
     LSQPushTooViewController *too = [[LSQPushTooViewController alloc]init];
    // LSQpushModel *model = [[LSQpushModel alloc]init];
    // model = [self.arrayInterestToo objectAtIndex:indexPath.row];
     too.imageUrl = model.pic;
     [self.navigationController pushViewController:too animated:YES];
     }
 }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
